package com.xxl.job.executor.service.jobhandler;

import com.hns.its.common.enumerate.DriverStatus;
import com.hns.its.common.utils.AppUtils;
import com.hns.its.domain.ItsSysUserLeaveEntity;
import com.hns.its.domain.ItsTransportDriverEntity;
import com.hns.its.service.driverAdmin.ItsOrgEmployeeLeaveService;
import com.hns.its.service.driverAdmin.ItsTransportDriverService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 任务Handler示例（Bean模式）
 * 
 * 开发步骤： 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”；
 * 2、注册到Spring容器：添加“@Component”注解，被Spring容器扫描为Bean实例；
 * 3、注册到执行器工厂：添加“@JobHandler(value
 * ="自定义jobhandler名称")”注解，注解value值对应的是调度中心新建任务的JobHandler属性的值。 4、执行日志：需要通过
 * "XxlJobLogger.log" 打印执行日志；
 * 
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHandler(value = "employeeLeaveJobHandler")
@Component
public class EmployeeLeaveJobHandler extends IJobHandler {

	@Autowired
	ItsTransportDriverService itsTransportDriverService;
	@Autowired
	ItsOrgEmployeeLeaveService leaveService;

	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("更新司机空闲状态......开始");

		List<ItsSysUserLeaveEntity> complateList = leaveService.complateList();
		for (int j = 0; j < complateList.size(); j++) {
			ItsSysUserLeaveEntity temp = complateList.get(j);
			ItsTransportDriverEntity driver = itsTransportDriverService.findDriver(temp.getEmpId());
			if (AppUtils.isNotEmpty(driver)) {
				driver.setDriverStatusDic(DriverStatus.FREE.get());
				itsTransportDriverService.updateItsTransportDriverEntity(driver);
			}
		}
		// 更新司机请假状态
		List<ItsSysUserLeaveEntity> applyList = leaveService.applyLeave();
		for (int j = 0; j < applyList.size(); j++) {
			ItsSysUserLeaveEntity temp = applyList.get(j);
			ItsTransportDriverEntity driver = itsTransportDriverService.findDriver(temp.getEmpId());
			if (AppUtils.isNotEmpty(driver)) {
				driver.setDriverStatusDic(DriverStatus.LEAVE.get());
				itsTransportDriverService.updateItsTransportDriverEntity(driver);
			}
		}

		XxlJobLogger.log("更新司机空闲状态......结束");

		return new ReturnT<String>(ReturnT.SUCCESS_CODE, "更新司机空闲状态成功！");
	}

}
